import { Component } from '@angular/core';
import {NavController, ModalController, AlertController, ToastController} from 'ionic-angular';
import {SignOrlogin} from '../sign-orlogin/sign-orlogin'
import {UserInfoPage} from "../user-info/user-info";
import {ScanPage} from "../scan/scan";
import {VersionsPage} from "../versions/versions";
import { StorageService } from "../../providers/StorageService";
import {UserInfo} from "../../model/UserInfo";

import {USERFACE_PATH} from "../../providers/Constants"

import { BaseUI } from '../../common/baseui';
import {MySportsPage} from "../my-sports/my-sports";
import {SportService} from "../../providers/SportService";


@Component({
  selector: 'page-usercenter',
  templateUrl: 'usercenter.html'
})
export class UsercenterPage extends BaseUI{
	  params: Object;
  	pushPage: any;
    islogin: boolean;
    userInfo:UserInfo;
    userFaceImg:any;
    sportList:any;
  constructor(
  	public navCtrl: NavController,
  	public modalCtrl: ModalController,
    private storageService: StorageService,
    public alertCtrl:AlertController,
    public toastCtrl: ToastController,
    public  sportService:SportService
  	) {

    super();

  }
  //打开注册/登陆
  openSignupOrlogin(){
  	let myModal = this.modalCtrl.create(SignOrlogin);
   	myModal.present();
  }

  //用户注销
  // logout() {
  //   this.storageService.remove('UserInfo');
  //  this.checkLogin();
  // }

  ionViewDidEnter() {
    console.log("进入个人中心页面");
    this.checkLogin();//加载页面前检查用户是否登录
  }

  //检查是否登录
  checkLogin(){
    if (this.storageService.read<UserInfo>('UserInfo')!=null) {
      console.log(this.storageService.read<UserInfo>('UserInfo'));
      this.userInfo = this.storageService.read<UserInfo>('UserInfo');
      console.log(this.userInfo.phone);
      this.userFaceImg = USERFACE_PATH+"/"+this.userInfo.userFace;
      this.islogin=true;//修改
    }else{
      this.islogin=false;
    }
  }

  /**
   * 跳转到扫描二维码的页面，加上 animate = false 的参数是为了
   * 相机能够在整个屏幕中显示，如果不加，相机就出不来。
   * animate 的参数默认值为 true
   *
   * @memberof MorePage
   */
  gotoScanQRCode() {
    this.navCtrl.push(ScanPage, null, { "animate": false });
  }

  gotoUserInfoPage() {
    this.navCtrl.push(UserInfoPage);
  }

  gotoVersions() {
    this.navCtrl.push(VersionsPage);
  }
  //清除缓存
  clearCache(){
    let confirm = this.alertCtrl.create({
      message: '确认要清除缓存?',
      buttons: [
        {
          text: '取消',
          handler: () => {
            // console.log('Disagree clicked');
          }
        },
        {
          text: '确定',
          handler: () => {
            localStorage.removeItem('firstIn');
            super.showToast(this.toastCtrl, "缓存清除成功。",'bottom');
          }
        }
      ]
    });
    confirm.present();
  }

  //我发布的活动
  pubSportFromMyself(){
    this.sportService.selectListByUserId(this.userInfo.id).subscribe(res => {
      this.navCtrl.push(MySportsPage,{title:'我发布的活动',sportList:res['data']});
    })
  }
  //我参加的活动
  slectIJoinSports(){
    this.sportService.selectIJoinSports(this.userInfo.id).subscribe(res => {
      this.navCtrl.push(MySportsPage,{title:'我参加的活动',sportList:res['data']});
    })
  }

}
